/**
 * @description: axios封装
 * @param {*} channel 店铺类型  3 淘宝  6 天猫 / 接口域名
 * @param {*} url 接口
 * @param {*} data 入参
 * @param {*} method 接口类型
 * @param {*} headers 请求头
 * @return {*}
 */
function request(channel = globalData.channel, url = "", data = null, method = "get", headers = null) {
    const axiosInstance = axios.create()
    calcRequestTimes(new Date().getTime())
    if(typeof channel === 'number' && channel){
        axiosInstance.defaults.baseURL = tabTypes.find(
          (item) => item.channel === channel
        ).baseURL;
    }else if(typeof channel === 'string' && channel){
        axiosInstance.defaults.baseURL = channel
    }
  axiosInstance.defaults.timeout = 10000;
  axiosInstance.defaults.headers.post["Content-Type"] = "application/json";
  if(headers && Object.keys(headers).length){
    Object.keys(headers).forEach(key => {
      axiosInstance.defaults.headers.common[key] = headers[key]
    })
  }
  const config = {};
  config["method"] = method;
  config["url"] = url;
  switch (method) {
    case "get":
      config["params"] = data;
      break;
    case "post":
      config["data"] = data;
  }

  return new Promise((resolve, reject) => {
    axiosInstance(config)
      .then((res) => {
        if (res.status === 200) {
          resolve(res.data);
        } else {
          reject(false);
        }
      })
      .catch((err) => {
        reject(err);
      });
  });
}
